package top.chmin.project.cloud.common.entity;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * chmin
 * 2023/12/19 13:33
 * cloud-builder
 */
public abstract class StringFPCField<T extends PreCondition> {

    T t;
    String columnName;

    public StringFPCField(T t, String columnName) {
        this.t = t;
        this.columnName = columnName;
    }

    public T eq(String value) {
        t.addSql(columnName + " = ?", value);
        return t;
    }

    public T ne(String value) {
        t.addSql(columnName + " <> ?", value);
        return t;
    }

    public T like(String value) {
        t.addSql(columnName + " like CONCAT('%%', ?, '%%')", value);
        return t;
    }

    public T leftLike(String value) {
        t.addSql(columnName + " like CONCAT(?, '%%')", value);
        return t;
    }

    public T rightLike(String value) {
        t.addSql(columnName + " like CONCAT('%%', ?)", value);
        return t;
    }

    public T in(String... values) {
        t.addSql(columnName + " IN (%s)".formatted(
                Stream.of(values).map(t -> "?").collect(Collectors.joining(", "))
        ), values);
        return t;
    }

    public T in(List<String> values) {
        t.addSqlString(columnName + " IN (%s)".formatted(
                values.stream().map(t -> "?").collect(Collectors.joining(", "))
        ), values);
        return t;
    }

    public T notIn(String... values) {
        t.addSql(columnName + " NOT IN (%s)".formatted(
                Stream.of(values).map(t -> "?").collect(Collectors.joining(", "))
        ), values);
        return t;
    }

    public T notIn(List<String> values) {
        t.addSqlString(columnName + " NOT IN (%s)".formatted(
                values.stream().map(t -> "?").collect(Collectors.joining(", "))
        ), values);
        return t;
    }

    public T group() {
        t.addGroup(columnName);
        return t;
    }

    public T asc() {
        t.addOrder(columnName);
        return t;
    }

    public T desc() {
        t.addOrder(columnName + " DESC");
        return t;
    }

    public T select() {
        t.addSelect(columnName);
        return t;
    }

    public T as(String fieldName) {
        t.addSelect(columnName, fieldName);
        return t;
    }

    public T count() {
        t.addSelect("COUNT(%s)".formatted(columnName));
        return t;
    }

    public T countAs(String fieldName) {
        t.addSelect("COUNT(%s) AS %s".formatted(columnName, fieldName));
        return t;
    }
}
